<?php
class CmsSectionsController extends AppController {

	var $name = 'CmsSections';
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'quick_edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'quick_delete' => array('admin','manag')
	);	
	var $paginate = array(
        'fields' => array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status','Cm.id','Cm.name' ),
        'limit' => 25,        
        'order' => array(
            'CmsSection.cms_id' => 'asc'
        )
    );
    
   
	public function beforeRender(){
		parent::beforeRender();
		
		$this->layout = 'cms';
	}
	
	function index() {
		$this->CmsSection->recursive = 0;
		$this->set('cmsSections', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cms section', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cmsSection', $this->CmsSection->read(array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status','CmsSection.ctp_id','Cm.id','Cm.name'), $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->CmsSection->create();
			if ($this->CmsSection->save($this->data)) {
				$this->Session->setFlash(__('The cms section has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cms section could not be saved. Please, try again.', true));
			}
		}
		$cms = $this->CmsSection->Cm->find('list');
		$this->set(compact('cms'));
	}

	function quick_edit($controller=null,$action=null,$ctp_id=null){
		if (!empty($this->data)) {
			if ($this->CmsSection->save($this->data)) {
				$this->Session->setFlash(__('The cms section has been saved', true));				
			} else {
				$this->Session->setFlash(__('The cms section could not be saved. Please, try again.', true));				
			}
			$cm = $this->Cm->read(array('Cm.id','Cm.controller','Cm.action','Cm.name','Cm.keywords','Cm.description'), $this->data['CmsSection']['cms_id']);
			$this->redirect(array('action'=>'quick_edit',$cm['Cm']['controller'],$cm['Cm']['action'],$this->data['CmsSection']['ctp_id']));
		}		
		
		$cms_id = $this->Cm->field('id',array('Cm.controller'=>$controller,'Cm.action'=>$action));
		$cm = $this->Cm->read(array('Cm.id','Cm.controller','Cm.action','Cm.name','Cm.keywords','Cm.description'), $cms_id);
		
		if(is_numeric($cms_id)){
			$this->data = $this->CmsSection->find('first',
				array('conditions'=> array('cms_id'=>$cms_id,'ctp_id'=>$ctp_id),
					'fields'=>array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status','CmsSection.ctp_id','Cm.id','Cm.name')));
			if(empty($cm)){
				$new = array();
				$new['Cm']['controller'] = $controller;
				$new['Cm']['action'] = $action;
				$new['Cm']['name'] = ucwords(str_replace('_', ' ', $action));
				$new['Cm']['action'] = $action;
				$new['Cm']['keywords'] = ucwords(str_replace('_', ' ', $action));
				$new['Cm']['description'] = ucwords(str_replace('_', ' ', $action));
				$this->Cm->save($new);				
			}				
			if(empty($this->data)){
				$cmsSection['CmsSection']['cms_id'] = $cms_id;
				$cmsSection['CmsSection']['ctp_id'] = $ctp_id;
				$cmsSection['CmsSection']['body'] = __('Please enter here the content of the page',true);
				$this->_add_empty_section($cmsSection);	
			}	
		}else{
			$new = array();
			$new['Cm']['controller'] = $controller;
			$new['Cm']['action'] = $action;
			$new['Cm']['name'] = ucwords(str_replace('_', ' ', $action));
			$new['Cm']['action'] = $action;
			$new['Cm']['keywords'] = ucwords(str_replace('_', ' ', $action));
			$new['Cm']['description'] = ucwords(str_replace('_', ' ', $action));
		
			$this->Cm->create();
			if ($this->Cm->save($new)) {
				$cmsSection['CmsSection']['cms_id'] = $cms_id = $this->Cm->getLastInsertID();
				$cmsSection['CmsSection']['ctp_id'] = $ctp_id;
				$cmsSection['CmsSection']['body'] = __('Please enter here the content of the page',true);
				$this->_add_empty_section($cmsSection);
			} else {
				$this->Session->setFlash(__('The new cm could not be saved. Please, try again.', true));
			}
		}
		$this->layout = 'cms';
	}
	
	private function _add_empty_section($cmsSection){
		$this->CmsSection->create();
		if ($this->CmsSection->save($cmsSection)) {					
			// dodanie nowej sekcji				
			$this->data = $this->CmsSection->find('first',
				array('conditions'=> array('cms_id'=>$cmsSection['CmsSection']['cms_id'],'ctp_id'=>$cmsSection['CmsSection']['ctp_id']),
					'fields'=>array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status','CmsSection.ctp_id','Cm.id','Cm.name')));
			
			$this->Session->setFlash(__('The new cm has been saved', true));
		}
	}
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cms section', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->CmsSection->save($this->data)) {
				$this->Session->setFlash(__('The cms section has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cms section could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->CmsSection->read(array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status','Cm.id','Cm.name'), $id);
		}
		$cms = $this->CmsSection->Cm->find('list');
		$this->set(compact('cms'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cms section', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->CmsSection->delete($id)) {
			$this->Session->setFlash(__('Cms section deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cms section was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function quick_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cms section', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->CmsSection->delete($id)) {
			$this->Session->setFlash(__('Cms section deleted', true));
			$this->redirect($this->referer());
		}
		$this->Session->setFlash(__('Cms section was not deleted', true));
		$this->redirect($this->referer());
	}
}
?>